package com.rrqs.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.animation.AnimationSet;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.rrqs.frame.MyApplication;
import com.rrqs.util.Utils;
import com.zadtek.xmqishi.R;
import com.rrqs.activity.common.Const;
import com.rrqs.views.ProgressWebView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class UserAddressMap extends Activity {
	private ImageView backid;
	private TextView topview;
	
	ProgressWebView wxxy_web_lay;
	final Activity activity = this;
	private boolean isSuccess = true; // 访问wap页面是否成功
	private String wapUrl = Const.mURL+"/home/entry/emp_amap.html"; // wap的出现url
	private String wapTitle = ""; // wap页Title
	int startX = 0;
	int endX = 0;
	private static final int MENU_CONFIRM = 17;
	private static final int MENU_CONFIRM1 = 18;
	View view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		MyApplication.getInstace().addActivity(this);
		EventBus.getDefault().register(this);
		view = LayoutInflater.from(this).inflate(R.layout.user_address_map, null);
		setContentView(view);
		
		backid = (ImageView) findViewById(R.id.backid);
		topview = (TextView) findViewById(R.id.topview);
		topview.setText("配送热力图");
		backid.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		
		wxxy_web_lay = (ProgressWebView) view.findViewById(R.id.wxxy_web_lay);
		wxxy_web_lay.getSettings().setJavaScriptEnabled(true);
		wxxy_web_lay.getSettings().setSupportZoom(true);

		wxxy_web_lay.getSettings().setPluginState(PluginState.ON);

		// 设置加载进来的页面自适应手机屏幕
		wxxy_web_lay.getSettings().setUseWideViewPort(true);
		wxxy_web_lay.getSettings().setLoadWithOverviewMode(true);
		// 显示放大缩小控件
		wxxy_web_lay.getSettings().setBuiltInZoomControls(true);

		wxxy_web_lay.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				activity.setTitle("Loading...");
				activity.setProgress(progress * 100);
				if (progress == 100)
					activity.setTitle(R.string.app_name);
			}

			@Override
			public void onReceivedTitle(WebView view, String title) {
				super.onReceivedTitle(view, title);
				// wap_title.setText(title);
			}
		});
		wxxy_web_lay.setWebViewClient(new WebViewClient() {

			public void onReceivedError(WebView view, int errorCode,
					String description, final String failingUrl) { // Handle the
																	// error
				// 网络访问失败
				isSuccess = false;
				view.stopLoading();
			}

			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				// mProgress = BaseHelper.showProgress(XyzdBrowserActivity.this,
				// null, "加载中....", false, true);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
			}

		});
		wxxy_web_lay.loadUrl(wapUrl);

		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int mDensity = metrics.densityDpi;
		Log.v("maomao", "densityDpi = " + mDensity);
		if (mDensity == 240) {
			wxxy_web_lay.getSettings().setDefaultZoom(ZoomDensity.FAR);
		} else if (mDensity == 160) {
			wxxy_web_lay.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);
		} else if (mDensity == 120) {
			wxxy_web_lay.getSettings().setDefaultZoom(ZoomDensity.CLOSE);
		} else if (mDensity == DisplayMetrics.DENSITY_XHIGH) {
			wxxy_web_lay.getSettings().setDefaultZoom(ZoomDensity.FAR);
		} else if (mDensity == DisplayMetrics.DENSITY_TV) {
			wxxy_web_lay.getSettings().setDefaultZoom(ZoomDensity.FAR);
		} else {
			wxxy_web_lay.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);
		}
	}

	private static final int ZOOM_CONTROLS_TIMEOUT = (int) ViewConfiguration
			.getZoomControlsTimeout();

	private AnimationSet mAnimationSet;
	OnMenuItemClickListener listener = new OnMenuItemClickListener() {
		@Override
		public boolean onMenuItemClick(MenuItem item) {
			switch (item.getItemId()) {
			case MENU_CONFIRM:
				// ZoomControls mControls =new ZoomControls(MainActivity.this);
				// mControls.setZoomSpeed(50);
				// mControls.setIsZoomInEnabled(true);
				//
				// Toast.makeText(MainActivity.this, "您点击了放大按钮+=",
				// Toast.LENGTH_SHORT).show();
				// ZoomButtonsController mZoomButtonsController1 = new
				// ZoomButtonsController(view);
				// mZoomButtonsController1.setZoomOutEnabled(true);
				// mZoomButtonsController1.setZoomSpeed(50);

				break;
			case MENU_CONFIRM1:
//				Toast.makeText(MainActivity.this, "您点击了缩小按钮==",
//						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
			return false;
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (wxxy_web_lay.canGoBack()) {
				wxxy_web_lay.goBack();
				return true;
			} else {
				wxxy_web_lay.stopLoading();
				finish();
			}
		}else if (keyCode ==KeyEvent.KEYCODE_DPAD_UP) {//上
			
		}else if (keyCode ==KeyEvent.KEYCODE_DPAD_DOWN) {//下
			
		}else if (keyCode ==KeyEvent.KEYCODE_DPAD_LEFT) {//左
			
		}else if (keyCode ==KeyEvent.KEYCODE_DPAD_RIGHT) {//右
			
		}else if (keyCode ==KeyEvent.KEYCODE_ENTER) {//OK
			
		}else if (keyCode ==KeyEvent.KEYCODE_MENU) {//菜单
			
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		wxxy_web_lay.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();
		wxxy_web_lay.onResume();
	}
	@Subscribe
	public void onEventMainThread(final String event) {
		if (Utils.LOGINELSEWHERE.equals(event)){

			if (!MyApplication.getInstace().getTopActivity().contains("QsLoginActivity")){
				Intent intent = new Intent(UserAddressMap.this, QsLoginActivity_.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startActivity(intent);
				finish();
			}
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();

		EventBus.getDefault().unregister(this);
	}
}
